package com.hup.sidebar;

import android.util.Log;

import com.hup.sidebar.constant.PieFunctionConstant;
import com.hup.sidebar.framework.BaseApplication;
import com.hup.sidebar.framework.NotCrashExceptionHandler;
import com.hup.sidebar.framework.SubThreadHandler;
import com.hup.sidebar.framework.model.BaseConfig.ConfigException;
import com.hup.sidebar.framework.util.BuildConfigUtil;
import com.hup.sidebar.framework.util.ToastUtil;
import com.hup.sidebar.model.AppConfig;
import com.hup.sidebar.view.MainActivity;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * @author hugan
 * @date 2022/6/3
 */
public class SideBarApplication extends BaseApplication {
    private static final String TAG = SideBarApplication.class.getSimpleName();

    @Override
    public void onCreate() {
        super.onCreate();
        printStartLogo(TAG);
        initBeans();
        printStartedLogo(TAG);
    }

    private void initBeans() {
        if (BuildConfigUtil.getBuildType().startsWith("debug")) {
            //开发时,禁止安卓服务出现异常后自动重启/导致丢失错误日志
            NotCrashExceptionHandler.registerHandler();
        }

        ToastUtil.init(this);
        SideBarApplication.putBean(new SubThreadHandler("SubThreadHandler"))
                .setExceptionHandler(e -> Log.e("SubThreadHandler", "线程异常:", e));
        AppConfig appConfig = loadConfig();
        SideBarApplication.putBean(appConfig);
    }

    private AppConfig loadConfig() {
        AppConfig appConfig;
        try {
            appConfig = new AppConfig().loadAll(this);
            Log.i(TAG, "loadConfig: " + appConfig.simpleString());
            PieFunctionConstant.initFunctions(appConfig);
        } catch (ConfigException e) {
            //只发生在 数据被篡改
            Log.e(TAG, "配置异常", e);
            ToastUtil.short_(getString(R.string.config_error));
            //全部使用缺省值
            appConfig =  new AppConfig().loadAll(this, true);
            PieFunctionConstant.initFunctions(appConfig);
        }

        appConfig.applyPieColor();
        try {
            appConfig.noteDateFormatter = new SimpleDateFormat(appConfig.noteDateFormat, Locale.getDefault());
            appConfig.noteDateFormatter.format(new Date());
        } catch (Exception e) {
            Log.e(TAG, "loadConfig: noteDateFormat异常?:" + appConfig.noteDateFormat);
            appConfig.reset("noteDateFormat");
            appConfig.noteDateFormatter = new SimpleDateFormat(appConfig.noteDateFormat, Locale.getDefault());
        }
        appConfig.loadNoteData(this);
        return appConfig;
    }

    public static void onFloaterStarted(boolean b) {
        getBean(AppConfig.class).setFloaterStarted(b);
        executeIfExist(MainActivity.class, mainActivity -> mainActivity.onFloaterStarted(b));
    }

    public static void onAccessibilityStarted(boolean b) {
        SideBarApplication.getBean(AppConfig.class).setAccessibilityStarted(b);
        executeIfExist(MainActivity.class, mainActivity -> mainActivity.onAccessibilityStarted(b));
    }

}